package com.wuercloud.mq.message.sender;

import com.wuercloud.mq.message.sender.cache.MessageFaultCache;
import com.wuercloud.mq.common.ResultRes;
import com.wuercloud.mq.config.RabbitMqInInitializer;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created by JiangWL on 2017/3/23.
 */
@Service("wawaMqSendInvoker")
public class SendInvoker {

    @Autowired
    private RabbitMqInInitializer rabbitMqInInitializer;
    private WawaMQSendBuilder sendBuilder;
    @Autowired
    private MessageFaultCache messageFaultCache;
    @PostConstruct
    public void init() throws IOException, TimeoutException {
        sendBuilder = new WawaMQSendBuilder(messageFaultCache);
    }

    public ResultRes send(final Object msgObj,final String qName,final String expiration) {
        final RabbitTemplate rabbitTemplate = rabbitMqInInitializer.getTemplateMap().get(qName);
        return sendBuilder.buildSender(qName, rabbitTemplate).sendMessage(msgObj,expiration);
    }
}
